package com.dandan.tzx.activity;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.utils.ClassUtils;
import com.dandan.tzx.App;
import com.dandan.tzx.model.Person;
import com.dandan.tzx.otherwork.RouterPathConfig;
import com.dandan.tzx.router.DDRouter;
import com.dandan.tzx.routerhelper.R;
import com.dandan.tzx.service.FunServices;
import com.dandan.tzx.service.IService;

import java.io.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.Logger;

import dalvik.system.DexFile;


@Route(path = MainActivity.PATH, )
public class MainActivity extends BaseActivity implements View.OnClickListener{
    public final static String PATH = "/activity/MainActivity";
    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;
    private Button button6;
    private Button button7;

    //@Autowired(name= FunServices.PATH)
    IService service;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DDRouter.getInstance().inject(this);
        init();
        try {
            List<String> list = ClassUtils.getSourcePaths(App.getApp());
            for (String path:list) {
                Log.d("tanzhenxing", "s = " + path);
                DexFile dexfile = new DexFile(path);
                Enumeration<String> dexEntries = dexfile.entries();
                while (dexEntries.hasMoreElements()) {
                    String className = dexEntries.nextElement();
                    if (className.startsWith("com.dandan.tzx.router.")) {
                        Log.d("tanzhenxing---", className);
                    }
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            Log.d("tanzhenxing", e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("tanzhenxing", e.getMessage());
        }
    }

    NavCallback callback = new NavCallback() {
        @Override
        public void onArrival(Postcard postcard) {
            Log.d(TAG, "onArrival() called with: postcard = [" + postcard.getGroup() + "]");
        }

        @Override
        public void onInterrupt(Postcard postcard) {
            super.onInterrupt(postcard);
            Log.d(TAG, "onInterrupt() called with: postcard = [" + postcard.getPath() + "]");
        }

        @Override
        public void onLost(Postcard postcard) {
            super.onLost(postcard);
            Log.d(TAG, "onLost: " + postcard.getPath());
        }
    };

    protected void init() {
        setTitle(TAG);
        textView = (TextView) findViewById(R.id.text_view);
        textView.setText(this.getClass().getSimpleName());
        button = (Button) findViewById(R.id.button);
        button.setText("分组");
        button.setOnClickListener(this);
        button1 = (Button) findViewById(R.id.button1);
        button1.setText("M页");
        button1.setOnClickListener(this);
        button2 = (Button) findViewById(R.id.button2);
        button2.setText("参数");
        button2.setOnClickListener(this);
        button3 = (Button) findViewById(R.id.button3);
        button3.setText("暴露服务");
        button3.setOnClickListener(this);
        button4 = (Button) findViewById(R.id.button4);
        button4.setText("替换");
        button4.setOnClickListener(this);
        button5 = (Button) findViewById(R.id.button5);
        button5.setText("降级容错");
        button5.setOnClickListener(this);
        button6 = (Button) findViewById(R.id.button6);
        button6.setText("FRAGMENT");
        button6.setOnClickListener(this);
        button7 = (Button) findViewById(R.id.button7);
        button7.setText("跨模块调用");
        button7.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                DDRouter.getInstance().build(Example1Activity.PATH, "activity").navigation(App.getApp());
                break;
            case R.id.button1:
                DDRouter.getInstance().build(WebViewActivity.PATH).navigation();
                break;
            case R.id.button2:
                //DDRouter.getInstance().build(Uri.parse("router://dandanlove" + ParamsCallActivity.PATH + "?name=Tom&age=18&girl=true")).navigation();
                DDRouter.getInstance().build(ParamsCallActivity.PATH)
                        .withString("name", "Tome")
                        .withBoolean("girl", true)
                        .withInt("age", 18)
                        .withObject("obj", new Person("Jack", 19))
                        .navigation();
                break;
            case R.id.button3:
                service = DDRouter.getInstance().navigation(IService.class);
                service.showToast(null, FunServices.PATH);
                break;
            case R.id.button4:
                DDRouter.getInstance().replacePath(Example1Activity.PATH, WebViewActivity.PATH);
                break;
            case R.id.button5:
                DDRouter.getInstance().build("/activity/xxx").navigation(MainActivity.this);
                break;
            case R.id.button6:
                DDRouter.getInstance().build(FragActivity.PAHT).navigation(MainActivity.this);
                break;
            case R.id.button7:
                DDRouter.getInstance().build(RouterPathConfig.OMainActivity).navigation(MainActivity.this);
                break;
        }
    }
}
